import Vue from "vue"
import Vuex from "vuex"
import axios from "axios"


Vue.use(Vuex)

const store = new Vuex.Store({

    state: {
        footerShow: true,
        token: window.sessionStorage.token,
        islogin: window.sessionStorage.islogin ? window.sessionStorage.islogin : false,
        shopInfo: null,
        shopCount: 0
    },
    mutations: {
        changeFooterShow(state, flag) {
            state.footerShow = flag
        },
        setToken(state, params) {
            console.log(params);
            window.sessionStorage.token = state.token = params
            window.sessionStorage.islogin = state.islogin = true
        },
        setShopInfo(state, params) {
            state.shopCount = params.reduce((due, val) => {
                val.checked = true
                return due + val.count
            }, 0)
            state.shopInfo = params
            console.log(state.shopInfo);
        },
        changeShopListChecked(state, params) {
            state.shopInfo.map((val) => {
                val.checked = params
            })
        },
    },
    actions: {
        getShopInfo(state) {
            // console.log(state);
            axios({
                url: 'cart/list',
                method: 'get'
            }).then((res) => {
                console.log(res);
                if (res.data.data) {
                    state.commit('setShopInfo', res.data.data)
                }
            })
        }
    }

})
export default store